package com.htfyun.hteinkApi;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.SeekBar;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.htfyun.hteinkApi.databinding.ActivityMainBinding;

import hteink.api.EinkFactoryHelper;

public class MainActivity extends AppCompatActivity {
    private final String TAG = "MainActivity";

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        init();
    }

    private void init() {
        binding.tvDefaultDpi.setText("系统默认dpi为 " + EinkFactoryHelper.getDpiManager().getDefaultDpi());

        initTvGepDpi();
        initCheckBoxOther();
        initCheckBoxMine();
        initSeekBarOther();
        initSeekBarMine();
    }

    private void initTvGepDpi() {
        binding.tvGetDpi.setOnClickListener(v -> {
            String pkgNameOther = binding.etPkgName.getText().toString();
            if (!TextUtils.isEmpty(pkgNameOther)) {
                boolean dpiEnabled = EinkFactoryHelper.getDpiManager().isDpiEnabled(pkgNameOther);
                int dpi = EinkFactoryHelper.getDpiManager().getDpi(pkgNameOther);
                binding.cbOther.setChecked(dpiEnabled);
                binding.seekBarOther.setProgress(dpi);
            }
        });

    }

    private void initCheckBoxOther() {
        String pkgNameOther = binding.etPkgName.getText().toString();
        if (!TextUtils.isEmpty(pkgNameOther)) {
            boolean dpiEnabled = EinkFactoryHelper.getDpiManager().isDpiEnabled(pkgNameOther);
            binding.cbOther.setChecked(dpiEnabled);
        }
        binding.cbOther.setOnCheckedChangeListener((buttonView, isChecked) -> {
            String pkgName = binding.etPkgName.getText().toString();
            if (TextUtils.isEmpty(pkgName)) {
                Toast.makeText(getContext(), "请输入包名", Toast.LENGTH_SHORT).show();
                return;
            }
            EinkFactoryHelper.getDpiManager().setDpiEnabled(pkgName, isChecked);

            EinkFactoryHelper.getDpiManager().setDpi(pkgName, null, binding.seekBarOther.getProgress());

        });
    }

    private void initCheckBoxMine() {
        String pkgName = getPackageName();
        if (!TextUtils.isEmpty(pkgName)) {
            boolean dpiEnabled = EinkFactoryHelper.getDpiManager().isDpiEnabled(pkgName);
            binding.cbMine.setChecked(dpiEnabled);
        }
        binding.cbMine.setOnCheckedChangeListener((buttonView, isChecked) -> {
            String pkgName1 = getPackageName();
            EinkFactoryHelper.getDpiManager().setDpiEnabled(pkgName1, isChecked);

            EinkFactoryHelper.getDpiManager().setDpi(getPackageName(), getClassName(), binding.seekBarMine.getProgress());

        });
    }

    private void initSeekBarOther() {
        String pkgNameOther = binding.etPkgName.getText().toString();
        if (!TextUtils.isEmpty(pkgNameOther)) {
            int dpi = EinkFactoryHelper.getDpiManager().getDpi(pkgNameOther);
            binding.seekBarOther.setProgress(dpi);
            binding.seekBarOtherValue.setText(String.valueOf(dpi));
        }

        binding.seekBarOther.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                binding.seekBarOtherValue.setText(String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                final int progress = seekBar.getProgress();
                binding.seekBarOtherValue.setText(String.valueOf(progress));

                String pkgNameOther = binding.etPkgName.getText().toString();
                if (TextUtils.isEmpty(pkgNameOther)) {
                    Toast.makeText(getContext(), "请输入包名", Toast.LENGTH_SHORT).show();
                    return;
                }

                EinkFactoryHelper.getDpiManager().setDpi(pkgNameOther, null, progress);
            }
        });
    }

    private void initSeekBarMine() {

        int dpi = EinkFactoryHelper.getDpiManager().getDpi(getPackageName());
        binding.seekBarMine.setProgress(dpi);
        binding.seekBarMineValue.setText(String.valueOf(dpi));

        Log.e(TAG, "mine dpi = " + dpi);

        binding.seekBarMine.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                binding.seekBarMineValue.setText(String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                final int progress = seekBar.getProgress();
                binding.seekBarMineValue.setText(String.valueOf(progress));

                EinkFactoryHelper.getDpiManager().setDpi(getPackageName(), getClassName(), progress);
            }
        });
    }

    private String getClassName() {
        return MainActivity.this.getClass().getName();
    }

    private Context getContext() {
        return this;
    }
}